feat: support web search for zhipu via web_search_options#4516
feat: support web search for zhipu via web_search_options#4516feitianbubu wants to merge 1 commit intoQuantumNous:mainfrom
Conversation
WalkthroughThe changes add web search capability to the Zhipu API adapter by introducing a post-processing step in the OpenAI request conversion. A new helper function injects web search parameters into the Zhipu request payload when web search options are provided. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
relay/channel/zhipu_4v/relay-zhipu_v4.go (1)
71-76: Normalizesearch_context_sizebefore switching.Current matching is case-sensitive;
"Low"/"HIGH"silently miss the intended mapping. Normalizing input makes behavior more robust without changing semantics.Proposed refactor
- switch opts.SearchContextSize { + switch strings.ToLower(strings.TrimSpace(opts.SearchContextSize)) { case "low": ws["count"] = 5 case "high": ws["count"] = 15 ws["content_size"] = "high" }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@relay/channel/zhipu_4v/relay-zhipu_v4.go` around lines 71 - 76, Normalize opts.SearchContextSize before the switch so casing doesn't change behavior: convert opts.SearchContextSize to a canonical form (e.g., strings.ToLower/strings.ToLower(strings.TrimSpace(...))) and switch on that normalized value in the block that sets ws["count"] and ws["content_size"] (referencing opts.SearchContextSize, ws["count"], and ws["content_size"] to locate the code).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@relay/channel/zhipu_4v/relay-zhipu_v4.go`:
- Around line 78-80: The current code unconditionally appends a new web_search
tool to m["tools"], causing duplicates; instead, convert m["tools"] to a []any
(as done with tools), iterate its elements (expect map[string]any or
map[any]any), and if an element's "type" == "web_search" update that element's
"web_search" field with ws and set a flag; after the loop, if no existing
web_search was found append the new
map[string]any{"type":"web_search","web_search":ws}; finally assign the modified
slice back to m["tools"] (references: req.ToMap(), m, tools, "web_search").
---
Nitpick comments:
In `@relay/channel/zhipu_4v/relay-zhipu_v4.go`:
- Around line 71-76: Normalize opts.SearchContextSize before the switch so
casing doesn't change behavior: convert opts.SearchContextSize to a canonical
form (e.g., strings.ToLower/strings.ToLower(strings.TrimSpace(...))) and switch
on that normalized value in the block that sets ws["count"] and
ws["content_size"] (referencing opts.SearchContextSize, ws["count"], and
ws["content_size"] to locate the code).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 74be54c5-92e8-4b8c-baab-ea5d5e113fec
📒 Files selected for processing (2)
relay/channel/zhipu_4v/adaptor.gorelay/channel/zhipu_4v/relay-zhipu_v4.go
| m := req.ToMap() | ||
| tools, _ := m["tools"].([]any) | ||
| m["tools"] = append(tools, map[string]any{"type": "web_search", "web_search": ws}) |
There was a problem hiding this comment.
Avoid duplicating web_search tools in payload.
If tools already contains a type: "web_search" entry, this logic appends another one instead of updating it, which can produce an invalid/ambiguous upstream request.
Proposed fix
m := req.ToMap()
- tools, _ := m["tools"].([]any)
- m["tools"] = append(tools, map[string]any{"type": "web_search", "web_search": ws})
+ tools, _ := m["tools"].([]any)
+ updated := false
+ for i, t := range tools {
+ tool, ok := t.(map[string]any)
+ if !ok {
+ continue
+ }
+ if typ, _ := tool["type"].(string); typ == "web_search" {
+ tool["web_search"] = ws
+ tools[i] = tool
+ updated = true
+ break
+ }
+ }
+ if !updated {
+ tools = append(tools, map[string]any{"type": "web_search", "web_search": ws})
+ }
+ m["tools"] = tools
return m📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| m := req.ToMap() | |
| tools, _ := m["tools"].([]any) | |
| m["tools"] = append(tools, map[string]any{"type": "web_search", "web_search": ws}) | |
| m := req.ToMap() | |
| tools, _ := m["tools"].([]any) | |
| updated := false | |
| for i, t := range tools { | |
| tool, ok := t.(map[string]any) | |
| if !ok { | |
| continue | |
| } | |
| if typ, _ := tool["type"].(string); typ == "web_search" { | |
| tool["web_search"] = ws | |
| tools[i] = tool | |
| updated = true | |
| break | |
| } | |
| } | |
| if !updated { | |
| tools = append(tools, map[string]any{"type": "web_search", "web_search": ws}) | |
| } | |
| m["tools"] = tools |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@relay/channel/zhipu_4v/relay-zhipu_v4.go` around lines 78 - 80, The current
code unconditionally appends a new web_search tool to m["tools"], causing
duplicates; instead, convert m["tools"] to a []any (as done with tools), iterate
its elements (expect map[string]any or map[any]any), and if an element's "type"
== "web_search" update that element's "web_search" field with ws and set a flag;
after the loop, if no existing web_search was found append the new
map[string]any{"type":"web_search","web_search":ws}; finally assign the modified
slice back to m["tools"] (references: req.ToMap(), m, tools, "web_search").
|
这个适配新版的函数计费不 |
增加智谱官方支持的web search功能
📝 变更描述 / Description
智谱的web search 与 OpenAI的格式不兼容
https://docs.z.ai/api-reference/llm/chat-completion因此要进行转换
🚀 变更类型 / Type of change
将OpenAI标准的web search格式
转换为智谱的格式
📸 运行证明 / Proof of Work
请求:
返回:

Summary by CodeRabbit